<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/9 0009
 * Time: 下午 12:43
 */

namespace Admin\Model;


use Think\Model;
use Think\Page;

class SupplierModel extends Model{
    /**
     * 定义静态变量 $_validate
     * 自动验证
     */

    protected $_validate = array(
      array('name','require','供货商名字不能为空'),
        array('sort','number','排序为数字',2)
    );

    /**
     * 批量验证
     * @var bool
     */
    protected $patchValidate = true;

    /**
     * 获取分页
     * @return array $rows
     * @param array $cond
     */
    public function getPageResult($cond = array(),$page=1){
        $cond['status'] = array('gt',-1);
        //获取总条数
        $count = $this->where($cond)->count();
        //设置显示条数
        $size = C('PAGE_SIZE');
        //实例化page传参
        $page_obj = new Page($count,$size);
        //分页设置样式
        $page_obj->setConfig('theme',C('PAGE_THEME'));
        //显示html
        $page_html = $page_obj->show();
        $rows = $this->where($cond)->page($page,$size)->order('sort')->select();
        return array(
            'page_html'=>$page_html,
            'rows'=> $rows,
        );
    }

    /**
     * 逻辑删除字段
     * @param $id
     * @return bool
     */
   public function getDel($id){
       //构建字段和值
       $data = array(
           'status'=>'-1',
           'name'=>array('exp',"CONCAT(name,'_del')"),
       );

       return $this->where("id=$id")->setField($data);
   }
   
   public function getLast($find = '*'){
       return $this->field($find)->where(array('status'=>1))->select();
   }
}